/**
 */
package com.dingpeikeji.quickdeer.core.modules.activity.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.activity.entity.ActivityInfo;
import com.dingpeikeji.quickdeer.core.modules.activity.service.TActivityService;
import com.dingpeikeji.quickdeer.core.modules.cupons.dao.TCuponsDao;
import com.dingpeikeji.quickdeer.core.modules.cupons.service.TCuponsService;
import com.dingpeikeji.quickdeer.core.modules.driver.entity.TDrivers;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.line.service.TLineService;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;

/**
 * 活动Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/activity/tActivity")
public class TActivityController extends BaseController {

	@Autowired
	private TActivityService tActivityService;
	@Autowired
	private TOrdersService tOrdersService;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@ModelAttribute
	public ActivityInfo get(@RequestParam(required = false) String activityId) {

		if (StringUtils.isNotBlank(activityId)) {
			return null;// tCuponsService.get(activityId);
		} else {
			return new ActivityInfo();
		}
	}

	@RequestMapping(value = { "list", "" })
	public String list(ActivityInfo tActivityInfo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<ActivityInfo> page = tActivityService.findPage(new Page<ActivityInfo>(request, response), tActivityInfo);
		model.addAttribute("page", page);
		return "modules/activity/tActivityList";
	}

	//
	@RequestMapping(value = "form")
	public String form(ActivityInfo activityInfo,HttpServletRequest request, Model model) {
		String activityId= request.getParameter("activityId");
		if (activityId != null&&activityId.length()>0) {
			activityInfo = tActivityService.get(activityId);
		}
		model.addAttribute("activityInfo", activityInfo);
		return "modules/activity/tActivityForm";
	}

	@RequestMapping(value = "save")
	public String save(HttpServletRequest request, HttpServletResponse response,
			Model model, RedirectAttributes redirectAttributes) throws Exception {
		ActivityInfo activityInfo =new ActivityInfo();
		String activityId= request.getParameter("activityId");
		activityInfo.setActivityId(activityId);
		activityInfo.setCpName(request.getParameter("cpName")); 
		activityInfo.setMinNum(request.getParameter("minNum"));
		activityInfo.setTicket(request.getParameter("ticket"));
		activityInfo.setCarPrice(request.getParameter("carPrice"));
		activityInfo.setPeoplePrice(request.getParameter("peoplePrice"));
		activityInfo.setDateFrom(sdf.parse(request.getParameter("dateFrom")));
		activityInfo.setDateOver(sdf.parse(request.getParameter("dateOver")));
		tActivityService.updateActivity(activityInfo);
 		return "redirect:list";
	}
	
	@RequestMapping(value = "joinList")
	public String joinList(TOrders order,HttpServletRequest request,HttpServletResponse response, Model model) {
		Page<TOrders> page = tOrdersService.findPage(new Page<TOrders>(request, response), order);
		model.addAttribute("page", page);
		model.addAttribute("activityId", order.getActivityId());
		return "modules/activity/tJoinList";
	}
	
	@RequestMapping(value = "joinForm")
	public String joinForm(String orderid,String activityId,HttpServletRequest request, Model model) {
		TOrders order=tOrdersService.get(orderid);
		model.addAttribute("order", order);
		model.addAttribute("activityId", activityId);
		return "modules/activity/tJoinForm";
	}
	
	@RequestMapping(value = "joinSave")
	public String joinSave(TOrders order,HttpServletRequest request, HttpServletResponse response,
			Model model, RedirectAttributes redirectAttributes) throws Exception {
		String remark=request.getParameter("activityRemark");
		String orderid=request.getParameter("orderid");
		String activityId=request.getParameter("activityId");
		order=tOrdersService.get(orderid);
		order.setActivityRemark(remark);
		tOrdersService.update(order);
		
		TOrders order2=new TOrders();
		order2.setActivityId(activityId);
		Page<TOrders> page = tOrdersService.findPage(new Page<TOrders>(request, response), order2);
		model.addAttribute("page", page);
		model.addAttribute("activityId", order.getActivityId());
		return "modules/activity/tJoinList";
	}


}
